/* 
 * File:   Track.cpp
 * Author: dave
 * 
 * Created on 2010年11月2日, 下午5:03
 */
#include "Track.h"

extern int numFrame;
extern int hdims;
float hranges_arr[] = {0,180};
float* hranges = hranges_arr;
extern int minT;

Track::Track(Camera *camera) {
    this->cam = camera;
}

Track::Track(const Track& orig) {
}

Track::~Track() {
}

void Track::Clear()
{
    V.clear();
}

void Track::CamShift(trackObject obj)
{
    //hist = cvCreateHist(1, &hdims, CV_HIST_ARRAY, &hranges, 1);
    //histImg = cvCreateImage(cvSize(320, 200), 8, 3);
    //cvZero(histImg);

    //搜索边框
    window = cvRect(obj.pt0.x, obj.pt0.y, obj.pt1.x - obj.pt0.x, obj.pt1.y - obj.pt0.y);

    //计算目标直方图的反向投影
    cvCalcBackProject(&cam->hue, cam->backProject, cam->hist);
    //蒙板
    //cvAnd(backProject, mask, backProject, 0);
    //执行CamShift算法跟踪目标
    cvCamShift(cam->backProject, window,
            cvTermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ),
            &comp, &box);
    //保存跟踪结果
    V.push_back(comp.rect);
    //cvReleaseHist(hist);
}

void Track::Draw(IplImage * image)
{
    for(int i = 0; i < V.size(); ++i)
    {
        CvPoint pt0, pt1;
        pt0.x = V[i].x;
        pt0.y = V[i].y;
        pt1.x = V[i].x + V[i].width;
        pt1.y = V[i].y + V[i].height;
        cvRectangle(image, pt0, pt1, CV_RGB(255,0,0), 3);
    }
}
